package com.bjc.principle.composite;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: DesignPattern
 * @description: 容器
 * @author: bjc
 * @create: 2021-01-04 17:19
 */
public class Composite implements Counter{

    private List<Counter> counterList = new ArrayList<>();

    public void add(Counter counter){
        counterList.add(counter);
    }

    public void delete(Counter counter){
        counterList.remove(counter);
    }

    public List<Counter> getChild(){
        return counterList;
    }

    @Override
    public int count() {
        int sum = 0;
        System.out.println("---"+counterList.size());
        for (Counter counter : counterList) {
            sum+= counter.count();
        }
        return sum;
    }
}
